package com.ninong.ker.common.oss.upload;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectRequest;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidBucketNameException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;

/**
 * 
 * @author 鸠摩智
 *
 */
public class OssUpload implements IOssUpload {


	@Override
	public boolean doesObjectExits(OSSClient ossClient, String bucketName, String yourKey)  {
		return ossClient.doesObjectExist(bucketName, yourKey);
	}

	@Override
	public String uploadString(OSSClient ossClient, String bucketName, String yourKey, String prefix, String content)
			 {
		return uploadByte(ossClient, bucketName, yourKey, prefix, content.getBytes());
	}

	@Override
	public String uploadByte(OSSClient ossClient, String bucketName, String yourKey, String prefix, byte[] bytes)
			 {
		ossClient.putObject(bucketName, yourKey, new ByteArrayInputStream(bytes));

		return prefix + yourKey;
	}

	@Override
	public String uploadInputStream(OSSClient ossClient, String bucketName, String yourKey, String prefix,
			InputStream inputStream)  {
		ossClient.putObject(bucketName, yourKey, inputStream);

		return prefix + yourKey;
	}

	@Override
	public String uploadFile(OSSClient ossClient, String bucketName, String yourKey, String prefix, File file)
			 {
		ossClient.putObject(new PutObjectRequest(bucketName, yourKey, file));

		return prefix + yourKey;
	}
	
	@Override
	public String uploadInputStream(MinioClient client, String bucketName, String objectName, InputStream inputStream) {
		try {
			client.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName)
					.stream(inputStream, inputStream.available(), -1).build());
		} catch (InvalidKeyException | ErrorResponseException | IllegalArgumentException | InsufficientDataException
				| InternalException | InvalidBucketNameException | InvalidResponseException | NoSuchAlgorithmException
				| ServerException | XmlParserException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			return client.getObjectUrl(bucketName, objectName);
		} catch (InvalidKeyException | ErrorResponseException | IllegalArgumentException | InsufficientDataException
				| InternalException | InvalidBucketNameException | InvalidResponseException | NoSuchAlgorithmException
				| ServerException | XmlParserException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return objectName;
	}

	@Override
	public String uploadInputStream(MinioClient client, String bucketName, String objectName, InputStream inputStream,
			String contentType) {
		try {
			client.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName)
					.stream(inputStream, inputStream.available(), -1).contentType(contentType).build());
		} catch (InvalidKeyException | ErrorResponseException | IllegalArgumentException | InsufficientDataException
				| InternalException | InvalidBucketNameException | InvalidResponseException | NoSuchAlgorithmException
				| ServerException | XmlParserException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			return client.getObjectUrl(bucketName, objectName);
		} catch (InvalidKeyException | ErrorResponseException | IllegalArgumentException | InsufficientDataException
				| InternalException | InvalidBucketNameException | InvalidResponseException | NoSuchAlgorithmException
				| ServerException | XmlParserException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return objectName;
	}
}
